constructive algorithms dp greedy strings

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;


#define take(x) int x;cin>>x;
#define give(x) cout <<x<<nline;
#define tvec(x,n) for (int i = 0; i < n; i++){cin >>x[i]; }
#define tvec1(x,n) for (int i = 1; i <= n; i++){cin >>x[i]; }
#define gvec(x,n) for (int i = 0; i < n; i++){cout <<x[i]<<" "; }cout<<nline;
#define gvec1(x,n) for (int i = 1; i <= n; i++){cout <<x[i]<<" "; }cout<<nline;
#define each(a, b) for (auto&(a) : (b))
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 998244353
#define inf 1e18
#define nline "\n"
#define LB lower_bound
#define UB upper_bound
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define setbits __builtin_popcountll
#define frontzero __builtin_clzll 
#define backzero __builtin_ctzll
#define parity __builtin_parityll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define reset(a, b) memset(a, b, sizeof(a))
template<typename S,typename T> void smax(S &a, const T &b) { if (a<b) a=b;}
template<typename S,typename T> void smin(S &a, const T &b) { if (a>b) a=b;}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(T arr[]);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
int mindfuk=1;
template <class T> void _print(T arr[]){cerr << "[ "; for (int i=0;i<mindfuk ;i++) {_print(arr[i]); cerr << " ";} cerr << "]";}



void comeon(){
    take(n);take(k);take(m);
    string s; cin>>s;
    int total=0;
    int cnt=0;string ans;
    int alpha[k];memset(alpha,0,sizeof(alpha));
    for (int i = 0; i < m; i++)
    {
        if(alpha[s[i]-'a']==0){
            cnt++;alpha[s[i]-'a']=1;
        }
        if(cnt==k){ans.push_back(s[i]);debug(i);
            total++;cnt=0;memset(alpha,0,sizeof(alpha));
        }
    }debug(ans);debug(total);
    if(total>=n){
        cout<<"yes"<<nline;return;
    }
    for (int i = 0; i < k; i++)
    {
        if(alpha[i]==0){char xx='a'+i;
            for (int j = 0; j < n-total; j++)
            {
                ans.pb(xx);
            } break;
        }
       
    }
give("no");give(ans);
    
}

int main() {
    fastio();
    
    int TC =1;
    cin >> TC;
    while(TC--) comeon();

}


Comments

Submit
0 Comments
More Questions

435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix